<?php

class Ads_Form_Placead extends Zend_Form {

    protected $_translate = null;

    public function init() {
        $this->_translate = Zend_Registry::get('Zend_Translate');
        $settings = new Ads_Model_Rentsetting();
        $zipDb = new Ads_Model_Zipcode();
//        $stateDb = new Ads_Model_State();
        $cityDb = new Ads_Model_City();

        $this->setMethod('post')
                ->setAttrib('enctype', 'multipart/form-data');

        $adTitle = new Zend_Form_Element_Text('adTitle');
        $adTitle->setLabel($this->_translate->_('headline') . ':')
                ->setOptions(array('size' => '50'))
                ->setAttribs(array('req' => 'req'))
                ->setRequired(true)
                ->addFilter('StringTrim');


        // create text input for address
        $adDesc = new Zend_Form_Element_Textarea('adDesc');
        $adDesc->setLabel($this->_translate->_('description') . ':')
                ->setOptions(array('rows' => '6', 'cols' => '36'))
                ->setRequired(true)
                ->setAttribs(array('req' => 'req'))
                ->addFilter('StringTrim');


        //address
        $houseNo = new Zend_Form_Element_Text('houseno');
        $houseNo->setLabel($this->_translate->_('house_no').':')
                ->setRequired(true)
                ->setAttribs(array('req' => 'req', 'errMsg' => $this->_translate->_('enter_house_no')))
                ->addFilter('StringTrim');

        //street name
        $streetName = new Zend_Form_Element_Text('streetName');
        $streetName->setLabel($this->_translate->_('street_name').':')
                ->setRequired(true)
                ->setAttribs(array('req' => 'req', 'errMsg' => $this->_translate->_('enter_street_name')))
                ->addFilter('StringTrim');

        //zip & city
        $zipcode = new Zend_Form_Element_Select('zipcode');
        $cityArray = $cityDb->fetchAll();
        $zipArray = $zipDb->fetchAll();
        $zipcode->setLabel($this->_translate->_('zip_city').':')
                ->setRequired(true)
                ->addValidator('Int');
        foreach ($zipArray as $value) {
            $zipId = $value->id;
            $zipCode = $value->codes;
            $cityId = $value->city_id;
            $cityName = $cityArray[$cityId - 1]['name'];
            $zipcode->addMultiOption($zipId, $zipCode . ' ' . $cityName);
        }


        $floor = new Zend_Form_Element_Text('floor');
        $floor->setLabel($this->_translate->_('floor').':')
//                ->setRequired(true)
                ->addFilter('StringTrim');

        //details of the lease

        $vacant_type = $settings->getVacantTypeArray();
        $vacant = new Zend_Form_Element_Radio('vacant');
        $vacant->setLabel($this->_translate->_('vacant').':')
                ->setRequired(true)
                ->addMultiOptions(array(
                    '0' => $this->_translate->_('immediately'),
                    '1' => $this->_translate->_('by_appointment'),
                    '2' => $this->_translate->_('select_date')
                ))
                ->setSeparator('');


        #date
        $date = new Zend_Form_Element_Text('date');
        $date->setLabel($this->_translate->_('vacant_date').':')
                ->setAttribs(array('id' => 'vac-date', 'placeholder' => $this->_translate->_('date_of_vacancy')))
                ->removeDecorator('Label')
                ->addFilter('StringTrim');


        $propertyType = new Zend_Form_Element_Select('propertyType');
        $ptypes = $settings->getProprertyTypeArray()->property_type;
        $pkeys = array_keys($ptypes);
        $propertyType->setLabel($this->_translate->_('select_property_type'))
                ->setRequired(true)
                ->addValidator('Int')
                ->addFilter('HtmlEntities');
        for ($index = 0; $index < count($ptypes); $index++) {
            $propertyType->addMultiOption($pkeys[$index], $ptypes[$index]);
        }


        $rentalPeriod = new Zend_Form_Element_Select('rentalPeriod');
        $rparray = $settings->getRentalPeriodArray()->rental_period;
        $rpkeys = array_keys($rparray);
        $rentalPeriod->setLabel($this->_translate->_('select_rental_type'))
                ->setRequired(true)
                ->addValidator('Int')
                ->addFilter('HtmlEntities');
        for ($index1 = 0; $index1 < count($rparray); $index1++) {
            $rentalPeriod->addMultiOption($rpkeys[$index1], $rparray[$index1]);
        }


        $area = new Zend_Form_Element_Text('area');
        $area->setLabel($this->_translate->_('area').':')
                ->setRequired(true)
                ->setAttribs(array('req' => 'req', 'errMsg' => $this->_translate->_('enter_area')))
                ->addFilter('HtmlEntities');

        $rooms = new Zend_Form_Element_Text('rooms');
        $rooms->setLabel($this->_translate->_('rooms').':')
                ->setRequired(true)
                ->setAttribs(array('req' => 'req', 'errMsg' => $this->_translate->_('enter_rooms')))
                ->addFilter('HtmlEntities')
                ->addValidator('Int');

        $rent = new Zend_Form_Element_Text('rent');
        $rent->setLabel($this->_translate->_('rent').':')
                ->setRequired(true)
                ->setAttribs(array('req' => 'req', 'errMsg' => $this->_translate->_('enter_rent_amount')))
                ->addFilter('HtmlEntities')
                ->setDecorators(array(
                    array('ViewHelper'),
                    array('Errors'),
                    array('HtmlTag', array('tag' => 'dd')),
                    array('Label', array('tag' => 'dt'))
        ));

        $furnishType = new Zend_Form_Element_Select('furnishType');
        $furarray = $settings->getFurnishTypeArray()->furnish_type;
        $furkey = array_keys($furarray);
        $furnishType->setLabel($this->_translate->_('select_furnish_type').':')
                ->setRequired(true)
                ->setOptions(array('class' => 'span4'))
                ->addFilter('HtmlEntities');
        for ($index3 = 0; $index3 < count($furarray); $index3++) {
            $furnishType->addMultiOption($furkey[$index3], $furarray[$index3]);
        }

        $depositeSelect = new Zend_Form_Element_Select('depositeSelect');
        $deparray = $settings->getDepositeTypeArray()->deposite_type;
        $depkey = array_keys($deparray);
        $depositeSelect->setLabel("Deposite:")
                ->setRequired(true)
                ->setOptions(array('class' => 'span4'))
                ->addFilter('HtmlEntities');
        for ($index2 = 0; $index2 < count($deparray); $index2++) {
            $depositeSelect->addMultiOption($depkey[$index2], $deparray[$index2]);
        }

        $depositeValue = new Zend_Form_Element_Text('depositeValue');
        $depositeValue->setRequired(true)
                ->setValue(0)
                ->setAttribs(array('req' => 'req', 'errMsg' => $this->_translate->_('enter_deposite_value')))
                ->setOptions(array('class' => 'span4'))
                ->addFilter('HtmlEntities')
                ->removeDecorator('Label');

        $prerentSelect = new Zend_Form_Element_Select('prerentSelect');
        $prerentSelect->setLabel($this->_translate->_('pre_paid_rent'))
                ->setRequired(true)
                ->setOptions(array('class' => 'span4'))
                ->addFilter('HtmlEntities');
        for ($index2 = 0; $index2 < count($deparray); $index2++) {
            $prerentSelect->addMultiOption($depkey[$index2], $deparray[$index2]);
        }

        $prerentValue = new Zend_Form_Element_Text('prerentValue');
        $prerentValue->setRequired(true)
                ->setValue(0)
                ->setAttribs(array('req' => 'req', 'errMsg' => $this->_translate->_('enter_pre_paid')))
                ->setOptions(array('class' => 'span4'))
                ->addFilter('HtmlEntities')
                ->removeDecorator('Label');

        $fixedExpense = new Zend_Form_Element_Text('fixedExpense');
        $fixedExpense->setLabel($this->_translate->_('fixed_expense').':')
//                ->setRequired(true)
                ->addFilter('HtmlEntities')
                ->addFilter('StringTrim');


        $pets_options = $settings->getPetsTypeArray();
        $pets = new Zend_Form_Element_Radio('pets');
        $pets->setLabel('Pets:')
                ->setRequired(true)
                ->addMultiOptions(array(
                    '0' => $this->_translate->_($pets_options->pets[0]),
                    '1' => $this->_translate->_($pets_options->pets[1])
                ))
                ->setSeparator('');

        $petsData = new Zend_Form_Element_Text('petsData');
        $petsData->setLabel($this->_translate->_('pets_data').':')
                ->setAttribs(array('id' => 'pets-data', 'placeholder' => $this->_translate->_('pets_placeholder')))
                ->addFilter('HtmlEntities');

        //image upload
        $photo = new Zend_Form_Element_File('photo');
        $photo->setLabel($this->_translate->_('pictures').':')
                ->addValidator('IsImage')
                ->addValidator('Size', false, 204800)
                ->addValidator('Extension', false, 'jpg,png,gif')
//                ->setDestination('/var/lib/openshift/5155ee6f4382ecc26a00023f/app-root/runtime/repo/php/uploads')
                ->setDestination('/var/www/rent-zend/public/uploads')
                ->setValueDisabled(true)
                ->setAttrib('multiple', 'multiple')
                ->setIsArray(true)
                ->removeDecorator('Label');



        // create submit button
        $submit = new Zend_Form_Element_Submit('submit');
        $submit->setLabel($this->_translate->_('create_new_property'))
                ->setOptions(array('class' => 'submit btn btn-success'));

        // attach elements to form    
        $this->addElements(array($adTitle, $adDesc));

        // create display group for ad details shown on search...
        $this->addDisplayGroup(array('adTitle', 'adDesc'), 'addesc');
        $this->getDisplayGroup('addesc')
                ->setLegend($this->_translate->_('ad_text'));

        //create display group for address of property.
        $this->addElements(array($zipcode, $streetName, $houseNo, $floor));
        $this->addDisplayGroup(array('zipcode', 'streetName', 'houseno', 'floor'), 'adAddress');
        $this->getDisplayGroup('adAddress')
                ->setLegend($this->_translate->_('address_of_property'));

        //create display group for full details of lease
        $this->addElements(array($vacant, $date, $propertyType, $rentalPeriod, $area, $rooms, $rent, $furnishType, $depositeSelect, $depositeValue, $prerentSelect, $prerentValue, $fixedExpense, $pets, $petsData));
        $this->addDisplayGroup(array('vacant', 'date', 'propertyType', 'rentalPeriod', 'area', 'rooms', 'rent', 'furnishType', 'depositeSelect', 'depositeValue', 'prerentSelect', 'prerentValue', 'fixedExpense', 'pets', 'petsData'), 'leaseDetails');
        $this->getDisplayGroup('leaseDetails')
                ->setLegend($this->_translate->_('details_of_lease'));


        $this->addElements(array($photo));
        $this->addDisplayGroup(array('photo'), 'photos');
        $this->getDisplayGroup('photos')
                ->setLegend($this->_translate->_('pics_of_property'));

        #id
        $id = new Zend_Form_Element_Hidden('propertyId');
        $id->setValue(0);
        // attach element to form    
        $this->addElements(array($id, $submit));
    }

}

